// pages/file/file.js
Page({
  data: {
    // 文件列表
    fileList: []

  },

  //保存文件
  saveFile() {
    wx.showLoading({
      title: '正在加载',
    })
    wx.chooseImage({
      success: (res) => {
        const tempFilePaths = res.tempFilePaths
        wx.saveFile({
          tempFilePath: tempFilePaths[0],
          success: (res) =>　{
            const savedFilePath = res.savedFilePath
            console.log(savedFilePath);
            wx.showToast({
              title: '成功保存',
            })
          }
        })
      },
      complete: () => {
        wx.hideLoading()  
      }
    })
  },


  // 获取本地文件列表
  getSaveFileList() {
    wx.showLoading({
      title: '正在获取中…',
    })
    wx.getSavedFileList({
      success: (result) => {
        this.setData({
          fileList: result.fileList
        })
        wx.showToast({
          title: '获取成功',
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 获取本地文件信息
  getSavedFileInfo() {
    wx.showLoading({
      title: '正在加载'
    })
    let {fileList} = this.data
    wx.getSavedFileInfo({
      filePath: fileList[0].filePath,
      success: (res) => {
        console.log(res)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  }, 

  // 下载文件并打开文档
  openDocument() {
    wx.downloadFile({
      url: 'http://circlesport.cn/doc/minipro.docx',
      success: (res) => {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath,
          success: (res) => {
            console.log(res)
          }
        })
      }
    })
  },

  // 删除图片
  removeImage(e) {
    let {index} = e.currentTarget.dataset
    let {fileList} = this.data
    fileList.splice(index,1)
    this.setData({
      fileList
    })

  }

})